package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.config.RedisInitRunner;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.pojo.*;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.vo.CheckinUserInfoVo;
import com.csun.cmny.provider.vo.OldManTypeStatusVo;
import com.csun.cmny.provider.vo.OldmanBedInfoVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 老人入院管理
 * */
@Slf4j
@RestController
@RequestMapping("/OldmanCheckIn")
public class OldmanCheckInController extends BaseController {
    @Autowired
    private OldmanCheckInService oldmanCheckInService;
    @Autowired
    private BedService bedService;
    @Autowired
    private OldmanNurseService oldmanNurseService;
    @Autowired
    private OldmanFeeService oldmanFeeService;
    @Autowired
    private OldmanRechargeService oldmanRechargeService;

    @Autowired
    private CacRpcService cacRpcService;

    @Autowired
    private OldmanNurseHiService oldmanNurseHiService;

    @Autowired
    private OldmanTendService oldmanTendService;

    @Autowired
    private OldmanLeaveService oldmanLeaveService;

    @Autowired
    private OldmanService oldmanService;

    @Autowired
    private ExpCtrItemService expCtrItemService;

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private RoomService roomService;

    @Resource
    private RedisInitRunner redisInitRunner;

    /**
     * 添加老人入院
     */
    @RequestMapping(value = "/addOldCheckIn", method = RequestMethod.POST)
    public Wrapper addOldCheckIn(@RequestBody OldmanCheckIn oldmanCheckIn) throws ParseException {

        Integer oldmanId = oldmanCheckIn.getOldmanId();
        //校验
        if (oldmanCheckIn.getOldmanId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人id不能为空", null);
        }
        if (oldmanCheckIn.getNursingLevel() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人护理级别不能为空", null);
        }
        if (oldmanCheckIn.getOldType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人服务类型不能为空", null);
        }
        if (org.apache.commons.lang.StringUtils.isBlank(oldmanCheckIn.getCheckInDate())) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人入住日期不能为空", null);
        }
        if (oldmanCheckIn.getSetMealFee() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "套餐费不能为空", null);
        }
        if (oldmanCheckIn.getSetMealId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "套餐id不能为空", null);
        }
        //如果是入院老人，绑定床位
        if (oldmanCheckIn.getOldType() == Constant.RUYUAN) {
            if (oldmanCheckIn.getBedId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人绑定的床位id不能为空", null);
            }
            if (oldmanCheckIn.getBedFee() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "床位费不能为空", null);
            }
            if (oldmanCheckIn.getBedItemId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "床位费项目id不能为空", null);
            }
        }

        if (oldmanCheckIn.getDeposit() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "押金不能为空", null);
        }
        if (oldmanCheckIn.getActPaidFee() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "实缴合计不能为空", null);
        }
        Oldman newoldman = new Oldman();
        newoldman.setOldmanId(oldmanCheckIn.getOldmanId());
        Oldman hasoldman = oldmanService.selectByOld(newoldman);
        if (hasoldman == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该老人还未登记或不存在", null);
        }
        // TODO 判断是否第二次入院
        OldmanCheckIn oldmanCheckIn1 = oldmanCheckInService.selectById(oldmanCheckIn.getOldmanId());
        if (oldmanCheckIn1 != null) {
            if (hasoldman.getStatus().equals(Constant.YIRUYUAN)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "该老人已经入院", null);
            } else {
                //删除入院记录
                int n = oldmanCheckInService.deleteByOldId(oldmanId);
                if (n > 0) {
                    try {
                        //删除老人设备信息
                        //int a = oldmanCheckInService.delOldDeviceByOid(oldmanId);
                        //删除老人私人定制信息
                        int b = oldmanCheckInService.delOldItemByOid(oldmanId);
                        if (b <= 0) {
                            logger.info("删除老人入院时删除老人私人定制信息失败");
                        }
                        //删除老人护理信息
                        int k = oldmanTendService.delOldTendByOid(oldmanId);
                        if (k <= 0) {
                            logger.info("删除老人入院时删除老人护理信息失败");
                        }
                        //删除老人护理记录
                        int c = oldmanNurseHiService.deleteByOldmanId(oldmanId);
                        if (c <= 0) {
                            logger.info("删除老人入院时删除老人护理记录失败");
                        }
                        //删除老人费用信息
                        int d = oldmanFeeService.deleteByOldmanId(oldmanId);
                        if (d <= 0) {
                            logger.info("删除老人入院时删除老人费用信息失败");
                        }
                        if (d > 0) {
                            //删除老人充值记录
                            int m = oldmanRechargeService.deleteByOldmanId(oldmanId);
                            if (m <= 0) {
                                logger.info("删除老人入院时删除老人充值记录失败");
                            }
                        }
                        //删除老人出院记录
                        int j = oldmanLeaveService.deleteByOid(oldmanId);
                        if (j <= 0) {
                            logger.info("删除老人入院时删除老人出院记录失败");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"删除成功",null);
                    log.error("入院时删除已出院信息成功");
                } else {
//                    return  WrapMapper.wrap(Wrapper.ERROR_CODE,"删除失败",null);
                    log.error("入院时删除已出院信息失败");
                }
            }

        }
        //获取当前登录用户
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long userId = loginAuthDto.getUserId();

        //修改者id
        Long modifier = userId;
        oldmanCheckIn.setModifier(modifier);
        oldmanCheckIn.setHomeId(loginAuthDto.getGroupId().intValue());

        //判断是哪类服务类别
        if (oldmanCheckIn.getOldType() == Constant.RUYUAN) {//入院老人
            //将老人和床位绑定
            Bed bed = new Bed();
            bed.setBedId(oldmanCheckIn.getBedId());
            bed.setOldmanId(oldmanCheckIn.getOldmanId());
            bed.setModifier(modifier);
            Bed bed1 = bedService.selectById(oldmanCheckIn.getBedId());
            if (bed1.getOldmanId() != null) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "该床位已经和老人绑定", null);
            }
            int n = bedService.updateById(bed);
            if (n <= 0) {
                logger.info("老人入院时将老人和床位绑定失败");
            }

            // 更新redis缓存
            redisInitRunner.refreshOldman(oldmanCheckIn.getOldmanId());
        } else if (oldmanCheckIn.getOldType() == 1) {//居家
            //生成楼栋、房间、床位(生成一个一层一  个房间一个床位的数据)
            //添加楼栋
            //暂且注释掉,居家老人不需要生成虚拟床位
           /* Building building = new Building();
            building.setModifier(modifier);
            building.setFloorNum(1);
            building.setBuildingName("居家老人楼栋" + "(" +  oldmanCheckIn.getOldmanId() + ")");
            building.setBuildingNo("JJLRLD");
            building.setBuildingType(2);
            building.setIsBuildNo(1);
            building.setBuildLink("-");
            int o = buildingService.insert(building);
            if (o <= 0) {
                logger.info("老人入院时初始化楼栋失败");
                return  WrapMapper.wrap(Wrapper.ERROR_CODE,"老人入院时初始化楼栋失败",null);
            }
            //添加房间
            Room room = new Room();
            room.setBuildingId(building.getBuildingId());
            room.setFloorNo(1);
            room.setBedNum(1);
            room.setModifier(modifier);
            room.setRoomNo("101");
            int p = roomService.insert(room);
            if (p <= 0) {
                logger.info("老人入院时初始化房间失败");
                return WrapMapper.error("老人入院时初始化房间失败");
            }
            //添加床位
            Bed bed = new Bed();
            bed.setRoomId(room.getRoomId());
            bed.setModifier(modifier);
            bed.setOldmanId(oldmanCheckIn.getOldmanId());
            bed.setBedNo(1);
            int q = bedService.insert(bed);
            if (q <= 0) {
                logger.info("老人入院时初始化床位失败");
                return  WrapMapper.wrap(Wrapper.ERROR_CODE,"老人入院时初始化床位失败",null);
            }*/
        }
        oldmanCheckIn.setStatus(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        oldmanCheckIn.setCreateTime(simpleDateFormat.parse(DateUtil.getCurrentDate()));
        oldmanCheckIn.setModifier(loginAuthDto.getUserId());
        //添加入院
        oldmanId = oldmanCheckInService.insert(oldmanCheckIn);
        if (oldmanId <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", null);
        }
        //插入日常护理表护理
        OldmanTend oldmanTend = new OldmanTend();
        oldmanTend.setOldmanId(oldmanId);
        oldmanTend.setModifier(loginAuthDto.getUserId());
        int k = oldmanTendService.insertSelective(oldmanTend);
        if (k < 0) {
            logger.info("入院时插入护理列表失败");
        }

        //插入出院表
        OldmanLeave oldmanLeave = new OldmanLeave();
        oldmanLeave.setOldmanId(oldmanId);
        oldmanLeave.setModifier(loginAuthDto.getUserId());
        int y = oldmanLeaveService.insertSelective(oldmanLeave);
        if (y < 0) {
            logger.info("入院时插入出院列表失败");
        }

        //老人私人定制项目集合
        List<OldmanItem> oldmanItemList = oldmanCheckIn.getOldmanItemList();
        //将老人和私人定制的服务绑定
        if (!CollectionUtils.isEmpty(oldmanItemList)) {
            for (int i = 0; i < oldmanItemList.size(); i++) {
                oldmanItemList.get(i).setOldmanId(oldmanCheckIn.getOldmanId());
                oldmanItemList.get(i).setModifier(modifier);
            }
            //插入老人私人定制项目关系表
            int a = oldmanCheckInService.insertOldItemBatch(oldmanItemList);
            if (a <= 0) {
                logger.info("入院时老人与私人定制项目绑定失败");
            }
        }
        return WrapMapper.ok();
    }

    //根据id删除老人入院信息
    @RequestMapping(value = "/delOldmanCheckIn", method = RequestMethod.POST)
    public Wrapper delOldmanCheckIn(Integer oldmanId) {
        if (oldmanId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人id不能为空", null);
        }
        //判断该老人的入院信息是否能够删除：只有当该老人为出院状态才可以删除
        OldmanCheckIn oldmanCheckIn = oldmanCheckInService.selectById(oldmanId);
        Oldman newoldman = new Oldman();
        newoldman.setOldmanId(oldmanId);
        Oldman oldman = oldmanService.selectByOld(newoldman);
        if (oldmanCheckIn == null || oldman == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", null);
        }

        if (Constant.YICHUYUAN != oldman.getStatus()) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该老人未出院,不能删除", null);
        }
        //删除入院记录
        int n = oldmanCheckInService.deleteByOldId(oldmanId);
        if (n > 0) {
            try {
                //删除老人设备信息
                //int a = oldmanCheckInService.delOldDeviceByOid(oldmanId);
                //删除老人私人定制信息
                int b = oldmanCheckInService.delOldItemByOid(oldmanId);
                if (b <= 0) {
                    logger.info("删除老人入院时删除老人私人定制信息失败");
                }
                //删除老人护理信息
                int k = oldmanTendService.delOldTendByOid(oldmanId);
                if (k <= 0) {
                    logger.info("删除老人入院时删除老人护理信息失败");
                }
                //删除老人护理记录
                int c = oldmanNurseService.delOldNurseByOid(oldmanId);
                if (c <= 0) {
                    logger.info("删除老人入院时删除老人护理记录失败");
                }
                //删除老人费用信息
                int d = oldmanFeeService.deleteByOldmanId(oldmanId);
                if (d <= 0) {
                    logger.info("删除老人入院时删除老人费用信息失败");
                }
                if (d > 0) {
                    //删除老人充值记录
                    int m = oldmanRechargeService.deleteByOldmanId(oldmanId);
                    if (m <= 0) {
                        logger.info("删除老人入院时删除老人充值记录失败");
                    }
                }
                //删除老人出院记录
                int j = oldmanLeaveService.deleteByOid(oldmanId);
                if (j <= 0) {
                    logger.info("删除老人入院时删除老人出院记录失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功", null);
        } else {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败", null);
        }
    }

    /**
     * 修改老人入院信息
     *
     * @param oldmanCheckIn
     * @return
     */
    @RequestMapping(value = "/updateOldCheckIn", method = RequestMethod.POST)
    public Wrapper updateOldCheckIn(@RequestBody OldmanCheckIn oldmanCheckIn) {
        if (oldmanCheckIn.getOldmanId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人id不能为空", null);
        }
        if (oldmanCheckIn.getNursingLevel() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人护理级别不能为空", null);
        }
        if (oldmanCheckIn.getOldType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人服务类型不能为空", null);
        }
        if (oldmanCheckIn.getSetMealFee() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "套餐费不能为空", null);
        }
        if (oldmanCheckIn.getSetMealId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "套餐id不能为空", null);
        }
        //如果是入院老人，绑定床位
        if (oldmanCheckIn.getOldType() == Constant.RUYUAN) {
            if (oldmanCheckIn.getBedId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人绑定的床位id不能为空", null);
            }
            if (oldmanCheckIn.getBedFee() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "床位费不能为空", null);
            }
        }
        if (oldmanCheckIn.getDeposit() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "押金不能为空", null);
        }
        if (oldmanCheckIn.getActPaidFee() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "实缴合计不能为空", null);
        }

        // 判断是否有修改实缴合计
        OldmanFee oldmanFee = oldmanFeeService.selectByOldmanId(oldmanCheckIn.getOldmanId());
        if (PublicUtil.isNotEmpty(oldmanFee)) {
            // 更新费用表
            oldmanFee.setRemainder(oldmanFee.getRemainder().add(oldmanCheckIn.getActPaidFee().subtract(oldmanFee.getActPaidFee())));
            oldmanFee.setActPaidFee(oldmanCheckIn.getActPaidFee());
            oldmanFeeService.updateByPrimaryKeySelective(oldmanFee);
        }

        Oldman oldman = new Oldman();
        oldman.setOldmanId(oldmanCheckIn.getOldmanId());
        Oldman hasoldman = oldmanService.selectByOld(oldman);
        if (hasoldman == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该老人还未登记或不存在", null);
        }
        OldmanCheckIn oldmanCheckIn1 = oldmanCheckInService.selectById(oldmanCheckIn.getOldmanId());
        if (oldmanCheckIn1 == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该老人还未入院", null);
        }
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long modifier = loginAuthDto.getUserId();
        //当老人修改为居家状态（oldType=1）时 解绑床位
        bedIf:
        if (oldmanCheckIn.getOldType() == 1) {
            //将老人和床位解绑
            oldmanCheckIn.setBedFee(new BigDecimal(0));
            bedService.updateByOid(oldmanCheckIn.getOldmanId());
        } else {
            OldmanBedInfoVo oldmanBedInfoVo = bedService.selectByOid(oldmanCheckIn.getOldmanId());
            //更换床位
            if (oldmanBedInfoVo != null) {
                //如果原本是入院老人且没有更换床位 则跳出床位IF判断
                if (oldmanCheckIn.getBedId() == oldmanBedInfoVo.getBedId()) {
                    break bedIf;
                }
                //将老人和旧床位解绑
                int result = bedService.updateByOid(oldmanCheckIn.getOldmanId());
                // 更新redis缓存
                redisInitRunner.refreshBed(oldmanBedInfoVo.getBedId());

                //将老人和新床位绑定
                Bed bed = new Bed();
                bed.setBedId(oldmanCheckIn.getBedId());
                bed.setOldmanId(oldmanCheckIn.getOldmanId());
                bed.setModifier(modifier);
                Bed bed1 = bedService.selectById(oldmanCheckIn.getBedId());
                if (bed1 != null) {
                    if (bed1.getOldmanId() != null) {
                        return WrapMapper.wrap(Wrapper.ERROR_CODE, "该床位已经和老人绑定", null);
                    }
                } else {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, "该床位不存在", null);
                }
                int n = bedService.updateById(bed);
                if (n <= 0) {
                    logger.info("老人入院时将老人和床位绑定失败");
                }
            } else {
                Bed b = new Bed();
                b.setBedId(oldmanCheckIn.getBedId());
                b.setOldmanId(oldmanCheckIn.getOldmanId());
                b.setModifier(modifier);
                Bed bed1 = bedService.selectById(oldmanCheckIn.getBedId());
                if (bed1 != null) {
                    if (bed1.getOldmanId() != null) {
                        return WrapMapper.wrap(Wrapper.ERROR_CODE, "该床位已经和老人绑定", null);
                    }
                }
                //将老人和新床位绑定
                Bed bed = new Bed();
                bed.setBedId(oldmanCheckIn.getBedId());
                bed.setOldmanId(oldmanCheckIn.getOldmanId());
                bed.setModifier(modifier);
                bedService.updateById(bed);
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            oldmanCheckIn.setModifyTime(sdf.parse(DateUtil.getCurrentDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //添加修改人ID
        oldmanCheckIn.setModifier(modifier);
        int k = oldmanCheckInService.updateSelective(oldmanCheckIn);

        if (k > 0) {
            //删除老人私人定制信息
            int c = oldmanCheckInService.delOldItemByOid(oldmanCheckIn.getOldmanId());
            //老人私人定制项目集合
            List<OldmanItem> oldmanItemList = oldmanCheckIn.getOldmanItemList();
            //将老人和私人定制的服务绑定
            if (!CollectionUtils.isEmpty(oldmanItemList)) {
                for (int i = 0; i < oldmanItemList.size(); i++) {
                    oldmanItemList.get(i).setOldmanId(oldmanCheckIn.getOldmanId());
                    oldmanItemList.get(i).setModifier(modifier);
                }
                //插入老人私人定制项目关系表
                int a = oldmanCheckInService.insertOldItemBatch(oldmanItemList);
                if (a <= 0) {
                    logger.info("老人入院修改老人与私人定制项目绑定失败");
                }
            }

            //

            // 更新redis缓存
            if (PublicUtil.isNotEmpty(oldmanCheckIn.getBedId())) {
                redisInitRunner.refreshBed(oldmanCheckIn.getBedId());
            }

            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", k);
        } else {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", null);
        }
    }

    /**
     * 根据老人id查询老人信息
     */

    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer oldmanId) {
        //校验
        if (oldmanId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人id不能为空", null);
        }
        OldmanCheckIn oldmanCheckIn = oldmanCheckInService.selectById(oldmanId);
        if (oldmanCheckIn == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", null);
        }
        //包含老人基本信息、评估信息、绑定的设备信息、私人定制项目信息
        Map<String, Object> oldmanMap = new HashMap<>();
        //老人基本信息
        CheckinUserInfoVo oldBaseInfoMap = oldmanCheckInService.selectByOid(oldmanId);
        //床位信息
        OldmanBedInfoVo oldmanBedInfo = bedService.selectByOid(oldmanId);

        //老人设备信息
        List<DeviceReqVo> devicemapList = new ArrayList<>();
//        List<OldmanDevice> oldmanDeviceList = oldmanCheckInService.sltOldDeviceByOid(oldmanId);
//        if (CollectionUtils.isEmpty(oldmanDeviceList)) {
//            oldmanDeviceList = new ArrayList<>();
//        }else {
//            //调用cmny-provider-dmc项目接口进行查询设备信息
//            for (OldmanDevice device:oldmanDeviceList) {
//                DeviceReqVo vo = dmcRpcService.getDeviceByid( device.getDeviceId().longValue());
//                devicemapList.add(vo);
//            }
//        }
        //老人私人定制项目信息
        List<Map<String, Object>> psnItemList = oldmanCheckInService.sltOldItemByOid(oldmanId);
        if (CollectionUtils.isEmpty(psnItemList)) {
            psnItemList = new ArrayList<>();
        }
        //查询老人评估信息
        List<Map<String, Object>> mapList = cacRpcService.showAll(oldmanId);
        oldmanMap.put("oldBaseInfoMap", oldBaseInfoMap);
        oldmanMap.put("oldmanBedInfo", oldmanBedInfo);
        oldmanMap.put("oldmanDeviceList", Lists.newArrayList());
        oldmanMap.put("psnItemList", psnItemList);
        oldmanMap.put("cacOldList", mapList);
        oldmanMap.put("devicemapList", devicemapList);

        return WrapMapper.ok(oldmanMap);
    }

    /**
     * 入院老人列表
     *
     * @param queryMap
     * @return
     */
    @RequestMapping(value = "/showAll", method = RequestMethod.POST)
    public Wrapper showAll(@RequestBody Map<String, Object> queryMap) {
        String pageNo = (String) queryMap.get("pageNo");
        String pageSize = (String) queryMap.get("pageSize");

        if (StringUtils.isEmpty(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        //当前登录用户获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        //Integer homeId = 1001;
        queryMap.put("homeId", homeId);
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        //查询得到的是基本信息列表
        List<Map<String, Object>> mapList = oldmanCheckInService.showAll(queryMap);

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(mapList);

       /* if (CollectionUtils.isEmpty(mapList)) {
            return new JsonResult(false, "没有查询到结果", new ArrayList<>());
        }*/
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 新增入院时所需的个性服务和套餐
     *
     * @return
     */
    @RequestMapping(value = "/getSetmealAndItem", method = RequestMethod.GET)
    public Wrapper getSetmealAndItem() {
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        int homeId = loginAuthDto.getGroupId().intValue();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("homeId", homeId);
        List<SetMeal> setMealList = setMealService.showAll(queryMap);
        //查询私人定制项目
        Map<String, Object> query1 = new HashMap<>();
        query1.put("expensectrType", Constant.GEXING);
        query1.put("homeId", homeId);
        Map<String, Object> query2 = new HashMap<>();
        query2.put("expensectrType", Constant.CHUANGWEI);
        query2.put("homeId", homeId);
        List<ExpCtrItem> gexingList = expCtrItemService.setlectItemByExpensectrType(query1);
        List<ExpCtrItem> chuangweiList = expCtrItemService.setlectItemByExpensectrType(query2);
        Map map = Maps.newHashMap();
        map.put("setMealList", setMealList);
        map.put("gexingList", gexingList);
        map.put("chuangweiList", chuangweiList);
        return WrapMapper.ok(map);
    }

    /**
     * 查询老人所有信息（入院信息，个人信息，评估信息，床位，个性化套餐）
     *
     * @param oldmanId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOldManAllInfo", method = RequestMethod.POST)
    public Wrapper queryOldManAllInfo(@RequestParam Integer oldmanId) {
        if (oldmanId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人id不能为空", oldmanId);
        }
        Map<String, Object> result = oldmanCheckInService.queryoldManAllInfo(oldmanId);
        return WrapMapper.ok(result);
    }




}
